package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.SpuImage;
import com.atguigu.gmall.product.entity.SpuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.entity.SpuSaleAttrValue;
import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo> implements SpuInfoService {

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    @Transactional
    @Override
    public void saveSpuInfoData(SpuInfoVo spuInfoVo) {
        //1 添加spuInfo
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuInfoVo,spuInfo);
        save(spuInfo);

        Long spuId = spuInfo.getId();
        //2 添加spuImage
        List<SpuImage> spuImageList = spuInfoVo.getSpuImageList().stream().map(item -> {
            SpuImage spuImage = new SpuImage();
            BeanUtils.copyProperties(item, spuImage);
            spuImage.setSpuId(spuId);
            return spuImage;
        }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImageList);

        //3 添加spuSaleAttr
        List<SpuSaleAttr> spuSaleAttrList = spuInfoVo.getSpuSaleAttrList().stream().map(item -> {
            SpuSaleAttr spuSaleAttr = new SpuSaleAttr();
            BeanUtils.copyProperties(item, spuSaleAttr);
            spuSaleAttr.setSpuId(spuId);
            return spuSaleAttr;
        }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrList);

        //4 添加spuSaleAttrValue
        List<SpuSaleAttrValue> spuSaleAttrValueList = spuInfoVo.getSpuSaleAttrList().stream().flatMap(item -> {
            return item.getSpuSaleAttrValueList().stream().map(val -> {
                SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();
                spuSaleAttrValue.setSpuId(spuId);
                spuSaleAttrValue.setSaleAttrName(item.getSaleAttrName());
                spuSaleAttrValue.setBaseSaleAttrId(val.getBaseSaleAttrId());
                spuSaleAttrValue.setSaleAttrValueName(val.getSaleAttrValueName());
                return spuSaleAttrValue;
            });
        }).collect(Collectors.toList());
        spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
    }
}




